/**
 * 获取地址栏指定参数
 * @param { String } name [参数名]
 * @param { String } url [地址]
 * @returns { String | null }
 */
 export function getUrlTargetParam (name,param) {
  var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)')
  var r = param.substr(1).match(reg)
  if (r != null) return unescape(r[2])
  return null
}

/**
 * 从url中获取参数
 * 
 * @example
 * import {getCode} from '@/util/index'
 * let pageData = this.getCode()
 * pageData={title:'',code:''}
 */
 export function getCode () {
  let url = ''
  // 子表单参数目前放在sessionStroage中，名称由地址栏中 sessionName 提供
  // eslint-disable-next-line no-undef
  const sessionName = getUrlTargetParam('sessionName', location.search)
  // eslint-disable-next-line no-undef
  const formParam = sessionName ? sessionStorage.getItem(sessionName) : ''
  if (formParam) {
    // eslint-disable-next-line no-undef
    url = location.search + '&' + formParam
  } else {
    url = window.location.search
    if (url.indexOf('?') === -1) {
      url = '?' + window.location.hash.split('?')[1]
    }
  }
  let obj = {}
  let reg = /[?&][^?&]+=[^?&]+/g
  let arr = url.match(reg)
  if (arr) {
    arr.forEach((item) => {
      let tempArr = item.substring(1).split('=')
      let key = decodeURIComponent(tempArr[0])
      obj[key] = decodeURIComponent(tempArr[1])
    })
  }

  return obj
}
